<template>
  <div>
    <div v-loading="loading">
      <div v-if="deliveries.length>0" class="alert alert-info">
        当前在您前面有 <span class="i-info"> {{deliveryOrder}} </span> 位等待发货.
      </div>
      <div v-if="deliveries.length==0">
        <el-table
          border
          tooltip-effect="dark"
          v-loading="loading"
        >
          <el-table-column
            label="订单编号" width="120" align="center"
          >
            <template slot-scope="scope">{{ scope.row.sn }}</template>
          </el-table-column>

          <el-table-column label="下单时间" width="160" prop="createTime" align="center">
          </el-table-column>

          <el-table-column
            label="商品信息" min-width="100">
            <template slot-scope="scope">
              <h-good-info-display v-model="scope.row" :displayInfo="false"></h-good-info-display>
            </template>
          </el-table-column>

          <el-table-column label="商品规格" min-width="100">
            <template slot-scope="scope">
              <h-specs-display v-model="scope.row"></h-specs-display>
            </template>
          </el-table-column>

          <el-table-column label="订单信息" min-width="200">
            <template slot-scope="scope">
              <h-order-info-display v-model="scope.row" :create-time="false"></h-order-info-display>
            </template>
          </el-table-column>

          <el-table-column
            label="订单金额"
            min-width="150">
            <template slot-scope="scope">
              <h-order-pay-info-display v-model="scope.row"/>
            </template>
          </el-table-column>

          <el-table-column label="状态" align="center" width="120">
            <template slot-scope="scope">
              <h-order-status-display v-model="scope.row" :dispatch="true" :arrival="true"
                                      :pay="true"></h-order-status-display>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div v-for="delivery in deliveries" :key="delivery.id" class="delivery-div">
        <fieldset>
          <legend>
            <div class="summary">
              <span class="label"> 发货单号:</span><span class="red"> {{delivery.sn}}</span>
              <span class="label"> 联系人:</span><span> {{delivery.concat}}</span>
              <span class="label"> 电话:</span><span> {{delivery.mobile}}</span>
              <span class="label"> 联系地址:</span><span> {{delivery.district}} {{delivery.address}}</span>
              <span class="label"> 邮编:</span><span> {{delivery.zipcode}}</span>
            </div>
          </legend>
          <el-table
            :data="delivery.details"
            border
            tooltip-effect="dark"
          >
            <el-table-column
              label="订单编号" width="120px" align="center"
            >
              <template slot-scope="scope">{{ scope.row.sn }}</template>
            </el-table-column>

            <el-table-column label="下单时间" width="160" prop="createTime" align="center">
            </el-table-column>

            <el-table-column
              label="商品信息" min-width="260">
              <template slot-scope="scope">
                <h-good-info-display v-model="scope.row" :displayInfo="false"></h-good-info-display>
              </template>
            </el-table-column>

            <el-table-column label="商品规格" min-width="100">
              <template slot-scope="scope">
                <h-specs-display v-model="scope.row"></h-specs-display>
              </template>
            </el-table-column>

            <el-table-column label="订单信息" min-width="200">
              <template slot-scope="scope">
                <h-order-info-display v-model="scope.row" :create-time="false"></h-order-info-display>
              </template>
            </el-table-column>

            <el-table-column
              label="订单金额"
              min-width="150">
              <template slot-scope="scope">
                <h-order-pay-info-display v-model="scope.row"/>
              </template>
            </el-table-column>

            <el-table-column label="状态" align="center" width="120">
              <template slot-scope="scope">
                <h-order-status-display v-model="scope.row" :dispatch="true" :arrival="true"
                                        :pay="true"></h-order-status-display>
              </template>
            </el-table-column>
          </el-table>
        </fieldset>
      </div>
      <div v-if="deliveries.length>0" class="row">
        <div class="col h-pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import HContainer from 'src/components/shop/layout/Container'
  import HDateRangePicker from 'src/components/common/HDateRangePicker'
  import FrontService from 'src/service/front'
  import HOrderStatusDisplay from 'src/components/common/display/HOrderStatusDisplay'
  import HGoodInfoDisplay from 'src/components/common/display/HGoodInfoDisplay'
  import HSpecsDisplay from 'src/components/common/display/HSpecsDisplay'
  import HOrderInfoDisplay from 'src/components/common/display/HOrderInfoDisplay'
  import HOrderPayInfoDisplay from 'src/components/common/display/HOrderPayInfoDisplay'

  export default {
    name: 'HApplyDisplay',
    components: {
      HContainer, HDateRangePicker, HOrderPayInfoDisplay,
      HOrderStatusDisplay, HGoodInfoDisplay, HSpecsDisplay, HOrderInfoDisplay
    },
    data() {
      return {
        search: {
          date: [],
          dispatch: 0,
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          needDetails: true
        },
        loading: false,
        deliveries: [],
        deliveryOrder: 0
      }
    },
    props: {
      date: {
        type: Array
      },
      type: {
        type: Number,
        default: 0
      }
    },
    watch: {
      type: function (c, d) {
        if (c == 1) {
          if (this.deliveries.length == 0) {
            this.getDatas();
            this.getOrder();
          }
        }
      }
    },
    methods: {
      handleSizeChange: function (size) {
        this.search.pagination.limit = size;
        this.search.pagination.page = 1;
        this.getDatas()
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page;
        this.getDatas();
      },
      getDatas: function () {
        const me = this
        me.loading = true;
        FrontService.deliveries(me.search).then(ds => {
          if (ds != null) {
            me.deliveries = ds.datas
            me.search.pagination = ds.pagination
          }
          me.loading = false;
        })
      },
      getOrder: function () {
        const me = this
        FrontService.deliveryOrder().then(ds => {
          if (ds != null) {
            me.deliveryOrder = ds.datas[0]
          }
        })
      },
      searchData: function () {
        const me = this;
        me.search.pagination.page = 1;
        me.search.date = me.date;
        me.getDatas();
      }
    }
  }


</script>
